package com.yzt.dynamicmodule.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.yzt.dynamicmodule.R;

/**
 * Des: 自定义圆形进度显示
 * Created by kele on 2020/9/10.
 * E-mail:984127585@qq.com
 */
public class RoundProgressView extends View {

    private int centerX;//圆环的中心X坐标
    private int centerY;//圆环的中心Y坐标
    private int radius;//圆环的半径
    private Paint paint;//圆环的画笔
    private Paint progressPaint;//圆环中进度的画笔
    private int progress;//当前进度
    private int maxProgress = 100;//最大进度，默认为100
    private float roundWidth;//进度圆环的宽度
    private int roundColor;//进度圆环的颜色
    private int roundProgressColor;//进度条的颜色

    public RoundProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(context, attrs);
        init();
    }

    public RoundProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressView(Context context) {
        this(context, null);
    }

    /**
     * 初始化自定义参数
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressView);
        roundWidth = ta.getDimension(R.styleable.RoundProgressView_round_width, 3);
        roundColor = ta.getColor(R.styleable.RoundProgressView_round_color, context.getResources().getColor(R.color.color_10FFFFFF));
        roundProgressColor = ta.getColor(R.styleable.RoundProgressView_round_progress_color, context.getResources().getColor(R.color.color_FF3535));
        maxProgress = ta.getInteger(R.styleable.RoundProgressView_round_max_progress, maxProgress);
        ta.recycle();
    }

    /**
     * 创建RoundProgressView时通过该方法初始化
     */
    private void init() {
        paint = new Paint();
        paint.setStrokeWidth(roundWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(roundColor);//灰色的圆环

        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setColor(roundProgressColor);//蓝色的进度
        progressPaint.setStrokeWidth(roundWidth);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);//让画笔画出的线条是圆的
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //计算圆环的圆心坐标
        centerX = getMeasuredWidth() / 2;
        centerY = getMeasuredHeight() / 2;
        //计算圆环的半径,减去进度条圆环宽度的一半
        radius = (centerX > centerY ? centerY : centerX) - dp2px(getContext(), roundWidth / 2);
    }

    public static int dp2px(Context context, float dpValue) {
        return (int) (dpValue * (context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画出灰色的圆环
        canvas.drawCircle(centerX, centerY, radius, paint);
        //构造圆环的外围矩形
        RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        //计算显示的进度
        int progressAngle = (progress * 360) / maxProgress;
        //画出进度，注意这里的270表示从圆环的最顶部开始画，如果270替换为0，则是从圆环的右端开始画
        canvas.drawArc(rectF, 270, progressAngle, false, progressPaint);
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public int getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }
}

